﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DAO;
using HRProject.Code.DTO;
using System.Collections.Generic;
using HRProject.Code.Common;

namespace HRProject
{
    public partial class AjaxUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sAction = Request.Params["action"];
            string sUserId = Request.Params["userId"];
            Guid userId;
            string result = "";
            try
            {
                userId = new Guid(sUserId);
                AuditDAO auditDao = new AuditDAO();
                if ("select_user".Equals(sAction))
                {
                    List<BLAudit> audits = auditDao.Search(userId);
                    result += "[";
                    int i = 0;
                    foreach (BLAudit a in audits)
                    {
                        if (i > 0)
                        {
                            result += ",";
                        }
                        result += "{\"audit\":\""+a.Key+"\"";
                        result += ",\"modes\":\"";
                        int j = 0;
                        foreach (BLAuditMode m in a.Modes)
                        {
                            if (m.IsAllowed)
                            {
                                if (j > 0)
                                {
                                    result += "|";
                                }
                                result += m.ModeKey;
                                j++;
                            }
                        }
                        result += "\"}";
                        i++;
                    }
                    result += "]";
                    result = "{\"status\":true,\"list\":"+result+"}";
                }
                else if ("update_audit".Equals(sAction))
                {
                    string auditArray = Request.Params["auditArray"];
                    string modeArray = Request.Params["modeArray"];
                    string[] audits = auditArray.Split(',');
                    string[] modeds = modeArray.Split(',');
                    int updateResult = 0;
                    int errorCode = 0;
                    if (audits.Length == modeds.Length)
                    {
                        for (int i = 0; i < audits.Length; i++)
                        {
                            updateResult = auditDao.UpdateAudit(userId, audits[i], modeds[i], ref errorCode);
                            if (errorCode == 12)
                            {
                                result = "{\"status\":false,\"error\":\"" + ContentProvider.GetMessage("NotAuthorizedDetail").Replace("{0}", "phân quyền cho tài khoản") + "\"}";
                                break;
                            }
                        }
                        if (errorCode == 0)
                        {
                            result = "{\"status\":true}";
                        }
                    }
                }
                else if ("remove_user".Equals(sAction))
                {
                    BLAudit audit = auditDao.Get(BLAuthentication.CurrentUserId, "user");
                    bool permission = false;
                    foreach (BLAuditMode mode in audit.Modes)
                    {
                        if ("remove".Equals(mode.ModeKey))
                        {
                            permission = true;
                            MembershipUser user = Membership.GetUser(userId);
                            bool updateResult = Membership.DeleteUser(user.UserName);
                            if (updateResult)
                            {
                                result = "{\"status\":true,\"message\":\"" + ContentProvider.GetMessage("SuccessfullyOperationDetail").Replace("{0}", "Xóa tài khoản") + "\"}";
                            }
                            else
                            {
                                result = "{\"status\":false,\"error\":\"" + ContentProvider.GetMessage("UnsuccessfullyOperationDetail").Replace("{0}", "Xóa tài khoản") + "\"}";
                            }
                            break;
                        }
                    }
                    if (!permission)
                    {
                        result = "{\"status\":false,\"error\":\"" + ContentProvider.GetMessage("NotAuthorizedDetail").Replace("{0}", "xóa tài khoản") + "\"}";
                    }
                }
            }
            catch (Exception exp)
            {
                Console.Out.WriteLine(exp.Message);
            }

            Response.Clear();
            Response.ContentType = "application/json; charset=utf-8";
            Response.Write(result);
            Response.End();
        }
    }
}
